﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ETL.Entity;
using ETL.Common.Logging;
namespace ETL.DataAccess
{
    public class ProjectUserRepository
    {


        //sql = "SELECT eng_id, eng_name, eng_type FROM project_all_names " +
        //         "WHERE eng_active = 1 ORDER BY eng_name";
        public IEnumerable<APP_USERS> GetAllActiveProjectUsers()
        {
            var db = new ETLDataEntities();
            var result = from s in db.APP_USERS
                             //where s.ENG_ACTIVE == 1 //true
                             orderby s.SURNNAME
                             select s;
            return result;
        }

        public IEnumerable<APP_USERS> GetFreeEntryAllowedActiveUsers()
        {
            var db = new ETLDataEntities();
            var result = from s in db.APP_USERS
                         //where s.ENG_ACTIVE == 1 && s.ENG_FREE_ENTRY_ALLOWED == 1
                         orderby s.SURNNAME
                         select s;
            return result;
        }

        /// <summary>
        /// Get user from userId
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public APP_USERS GetUserById(string userId)
        {
            try
            {
                var db = new ETLDataEntities();
                var result = (from s in db.APP_USERS
                              where s.DSE_ID.Equals(userId)
                              select s).FirstOrDefault();
                return result;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return null;
            }
        }

        /// <summary>
        /// Update selected project to user, the record's belong to table project_all_names
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="selectedProjectId"></param>
        /// <param name="selectedProjectName"></param>
        /// <returns></returns>
        public int UpdateSelectedProject(string userId, int? selectedProjectId, string selectedProjectName)
        {
            try
            {
                var db = new ETLDataEntities();
                var item = (from s in db.APP_USERS
                            where s.DSE_ID.Equals(userId)
                            select s).FirstOrDefault();

                if(item != null)
                {
                    //item.ENG_SELECTED_PROJECT = selectedProjectId;
                    //item.ENG_PROJECT_NAME = selectedProjectName;
                    db.Entry(item).State = EntityState.Modified;
                    int i = db.SaveChanges();
                    return i;
                }
                return 0;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return 0;
            }
        }

    }
}
